import logging
from logging.handlers import RotatingFileHandler
from .config import LOG_FILE

# Configure logger
logger = logging.getLogger("register_service")
logger.setLevel(logging.INFO)

# Prevent duplicate handlers if this module is imported multiple times
if not logger.handlers:
    # Create a rotating file handler
    handler = RotatingFileHandler(
        LOG_FILE,
        maxBytes=5 * 1024 * 1024,  # 5 MB
        backupCount=3,
        encoding="utf-8"
    )

    # Create a formatter and set it for the handler
    formatter = logging.Formatter(
        '%(asctime)s [%(levelname)s] %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S'
    )
    handler.setFormatter(formatter)

    # Add the handler to the logger
    logger.addHandler(handler)

    # Optional: Add a stream handler to also print logs to console
    # console_handler = logging.StreamHandler()
    # console_handler.setFormatter(formatter)
    # logger.addHandler(console_handler)

def log_success(msg):
    logger.info("✅ " + msg)

def log_error(msg):
    logger.error("❌ " + msg)

def log_warning(msg):
    logger.warning("⚠️ " + msg)

def log_info(msg):
    logger.info("ℹ️ " + msg)
